
using Youz.Common.DBUtility;
using Youz.Common.Entity;
using System;
using System.Data;
using System.Data.SqlClient;
using System.Text;

namespace Youz.Common.DAL
{
    /// <summary>
    /// CMS_Message Data Class
    /// </summary>
    public partial class CMS_MessageDAL : BaseDAL
    {
		private DbHelperSQL DbHelperSQL = new DbHelperSQL();
        public string TableName = "CMS_Message";

        public CMS_MessageDAL()
        {}

        #region  成员方法
        /// <summary>
        ///  新增一条数据
        /// </summary>
        public bool Add(CMS_Message model)
        {
            StringBuilder strSql = new StringBuilder();
			
            strSql.Append("insert into [CMS_Message](");
            strSql.Append("[Title],[Content],[MemberAccount],[IsRead],[Creator],[CreateTime]");
            strSql.Append(") values (");
            strSql.Append("@Title,@Content,@MemberAccount,@IsRead,@Creator,@CreateTime");
            strSql.Append(")");
			
            SqlParameter[] parameters = {
                  
                    new SqlParameter("@Title", model.Title),
                    new SqlParameter("@Content", model.Content),
                    new SqlParameter("@MemberAccount", model.MemberAccount),
                    new SqlParameter("@IsRead", model.IsRead),
                    new SqlParameter("@Creator", model.Creator),
                    new SqlParameter("@CreateTime", model.CreateTime)
            };

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        ///  保存一条数据
        /// </summary>
        public bool Update(CMS_Message model)
        {
            StringBuilder strSql = new StringBuilder();
			
            strSql.Append("update [CMS_Message] set ");
            strSql.Append("[Title]=@Title,[Content]=@Content,[MemberAccount]=@MemberAccount,[IsRead]=@IsRead");
            strSql.Append(" where [MessageID]=@MessageID");
			
            SqlParameter[] parameters = {
                  
                    new SqlParameter("@MessageID", model.MessageID),
                    new SqlParameter("@Title", model.Title),
                    new SqlParameter("@Content", model.Content),
                    new SqlParameter("@MemberAccount", model.MemberAccount),
                    new SqlParameter("@IsRead", model.IsRead)
            };

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public void SetAllRead(string memberAccount)
        {
            DbHelperSQL.ExecuteSql("update [CMS_Message] set [IsRead]=1 where [MemberAccount]=@MemberAccount", new SqlParameter[] { new SqlParameter("@MemberAccount", memberAccount) });
        }

        /// <summary>
        /// 删除一条数据
        ///</summary>
        public bool Delete(Int64 MessageID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from [CMS_Message] ");
            strSql.Append(" where [MessageID]=@MessageID");
			
            SqlParameter[] parameters = {
			
                new SqlParameter("@MessageID", MessageID)             
              
            };

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public CMS_Message Get(Int64 MessageID)
        {
            StringBuilder strSql = new StringBuilder();
			
            strSql.Append("select  top 1 * from [CMS_Message] ");
            strSql.Append(" where [MessageID]=@MessageID");
			
            SqlParameter[] parameters = {
			
                new SqlParameter("MessageID", MessageID)             
              
            };

            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return Get(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }

		/// <summary>
        /// 得到一个对象实体
        /// </summary>
        public CMS_Message Get(DataRow row)
        {
            if (row == null) return null;

            CMS_Message model = new CMS_Message();
	
            
            if (row["MessageID"] != DBNull.Value)
            {
                model.MessageID = (Int64)row["MessageID"];
            }      
            
            if (row["Title"] != DBNull.Value)
            {
                model.Title = (String)row["Title"];
            }      
            
            if (row["Content"] != DBNull.Value)
            {
                model.Content = (String)row["Content"];
            }      
            
            if (row["MemberAccount"] != DBNull.Value)
            {
                model.MemberAccount = (String)row["MemberAccount"];
            }      
            
            if (row["IsRead"] != DBNull.Value)
            {
                model.IsRead = (Boolean)row["IsRead"];
            }      
            
            if (row["Creator"] != DBNull.Value)
            {
                model.Creator = (String)row["Creator"];
            }      
            
            if (row["CreateTime"] != DBNull.Value)
            {
                model.CreateTime = (DateTime)row["CreateTime"];
            }      
            

            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet Gets(string sqlWhere)
        {
            StringBuilder strSql = new StringBuilder();
			
            strSql.Append("select * from [CMS_Message] ");
            if (sqlWhere.Trim() != "")
            {
                strSql.Append(" where " + sqlWhere);
            }
            
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet Gets(int Top, string sqlWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" *  FROM [CMS_Message] ");
			
            if (sqlWhere.Trim() != "")
            {
                strSql.Append(" where " + sqlWhere);
            }
            if(filedOrder.Trim() != "")
            {
                strSql.Append(" order by " + filedOrder);
            }
			
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string sqlWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM [CMS_Message] ");
            if (sqlWhere.Trim() != "")
            {
                strSql.Append(" where " + sqlWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

        /// <summary>
        /// 获取未读取消息数
        /// </summary>
        /// <param name="memberAccount"></param>
        /// <returns></returns>
        public int GetUnreadCount(string memberAccount)
        {
            return GetRecordCount(string.Format("isnull(IsRead,0)=0 and MemberAccount='{0}'", memberAccount));
        }

        /// <summary>
        /// 获得数据列表分页
        /// </summary>
        public DataTable Gets(string sqlWhere, int pageindex, int pagesize, out int total, params SqlParameter[] cmdParms)
        {
            StringBuilder strSql = new StringBuilder();

            strSql.Append(" SELECT * FROM [CMS_Message]  ");
            if (sqlWhere.Trim() != "")
            {
                strSql.Append(" where " + sqlWhere);
            }

            int count = 0;
            DataTable dt = DbHelperSQL.GetPager(pageindex, pagesize, "" + strSql + "", "order by MessageID desc ", out count, cmdParms);
            total = count;
            return dt;
        }
      #endregion  成员方法
    }
}


  